import { useState, useEffect } from 'react';
import { i18n, SupportedLanguage } from './i18n';

export function useTranslation() {
  const [currentLanguage, setCurrentLanguage] = useState<SupportedLanguage>(
    i18n.getCurrentLanguage()
  );

  useEffect(() => {
    // 监听语言变化
    const unsubscribe = i18n.addLanguageChangeListener((language) => {
      setCurrentLanguage(language);
    });

    return unsubscribe;
  }, []);

  const changeLanguage = async (language: SupportedLanguage) => {
    await i18n.setLanguage(language);
  };

  const t = (key: string, params?: Record<string, string | number>) => {
    return i18n.translate(key, params);
  };

  return {
    t,
    currentLanguage,
    changeLanguage,
    supportedLanguages: i18n.getSupportedLanguages()
  };
}